home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’91 / MS Works Merge Enhancer / SendPS.cp < prev    next >
Encoding:
Text File  |  1991-06-20  |  6.0 KB  |  243 lines  |  [TEXT/MPS ]

  1. #include <Types.h>
  2. #include <Strings.h>
  3. #include <Memory.h>
  4. #include <QuickDraw.h>
  5. #include <Errors.h>
  6. #include <Fonts.h>
  7. #include <Events.h>
  8. #include <Printing.h>
  9.  
  10. #include "THyperXCmd.h"        //our custom HyperCard class library
  11.  
  12. void DoPrintPS(TXCMDBlock *myParamPtr);
  13.  
  14. pascal void SendPS ( TXCMDBlock *myParamPtr )
  15. {
  16.  
  17.     THPrint        prRecHdl= nil;
  18.     TPPrPort    myPrPort=nil;
  19.     TPrStatus    myStRec;
  20.     short        theErr;
  21.     CString        errString;
  22.     
  23. /*
  24.   check for the correct number of HC parameters
  25. */
  26.  
  27.     if ( !myParamPtr->informArgNums((char *) "SendPS", 2) )
  28.         return;
  29.  
  30. /*
  31.   Recover the printer record and printing grafPort from the HC globals myPrintRecord
  32.   and myPrinterPort, respectively.
  33. */
  34.  
  35.     prRecHdl= (THPrint) myParamPtr->RecoverLongGlobal((StringPtr) "\pmyPrintRecord");
  36.     myPrPort= (TPPrPort) myParamPtr->RecoverLongGlobal((StringPtr) "\pmyPrinterPort");
  37.  
  38. /*
  39.   If the first HC argument is true, then close the document and exit, otherwise, proceed.
  40. */
  41.  
  42.     if (myParamPtr->RecoverBooleanArg(0))
  43.     {
  44.         if (prRecHdl)
  45.         {
  46.             PrCloseDoc(myPrPort);
  47.             if (theErr=PrError())
  48.             {
  49.                 errString= CString((char *)"PrCloseDoc failed. ") + CString((long) theErr);
  50.                 myParamPtr->SignalFatalError(errString);
  51.                 return;
  52.             }
  53.             myParamPtr->SetLongGlobal((StringPtr) "\pmyPrinterPort", (long) 0);
  54.             
  55.             if ( (**prRecHdl).prJob.bJDocLoop == bSpoolLoop )
  56.                 PrPicFile(prRecHdl,nil,nil,nil, &myStRec);
  57.             if (theErr=PrError())
  58.             {
  59.                 errString= CString((char *)"PrPicFile failed. ") + CString((long) theErr);
  60.                 myParamPtr->SignalFatalError(errString);
  61.                 return;
  62.             }
  63.         
  64.             PrClose();
  65.             if (theErr=PrError())
  66.             {
  67.                 errString= CString((char *)"PrClose failed. ") + CString((long) theErr);
  68.                 myParamPtr->SignalFatalError(errString);
  69.                 return;
  70.             }
  71.             
  72.             DisposHandle((Handle) prRecHdl);
  73.             myParamPtr->SetLongGlobal((StringPtr) "\pmyPrintRecord", nil);
  74.         }
  75.     
  76.     }
  77. /*
  78.   If the first parameter is false, and the print record handle is nil, create
  79.   the handle, initialize printing, and put up the job dialog.
  80. */
  81.     else if (!prRecHdl)
  82.     {
  83.         prRecHdl=(THPrint) NewHandle(sizeof(TPrint));
  84.         if (!prRecHdl)
  85.         {
  86.             myParamPtr->SignalFatalError((char *)"Out of memory!");
  87.             return;
  88.         }
  89.  
  90.         PrOpen();
  91.         
  92.         if (theErr=PrError())
  93.         {
  94.             errString= CString((char *)"PrOpen failed. ") + CString((long) theErr);
  95.             myParamPtr->SignalFatalError(errString);
  96.             return;
  97.         }
  98.     
  99.         if (!PrJobDialog(prRecHdl))
  100.         {
  101.             myParamPtr->SignalReturnStatus("\pCancel");
  102.             PrClose();
  103.             DisposHandle((Handle) prRecHdl);
  104.             return;
  105.         }
  106.         if (theErr=PrError())
  107.         {
  108.             errString= CString((char *)"PrJobDialog failed. ") + CString((long) theErr);
  109.             myParamPtr->SignalFatalError(errString);
  110.             PrClose();
  111.             DisposHandle((Handle) prRecHdl);
  112.             return;
  113.         }
  114.     
  115.         myParamPtr->SetLongGlobal((StringPtr) "\pmyPrintRecord", (long) prRecHdl);
  116.  
  117.         myPrPort = PrOpenDoc(prRecHdl,nil,nil);
  118.         if (theErr=PrError())
  119.         {
  120.             if (theErr==iPrAbort)
  121.             {
  122.                 myParamPtr->SignalReturnStatus("\pAbort");
  123.                 PrCloseDoc(myPrPort);
  124.             }
  125.             else
  126.             {
  127.                 errString= CString((char *)"PrOpenDoc failed. ") + CString((long) theErr);
  128.                 myParamPtr->SignalFatalError(errString);
  129.             }
  130.             DisposHandle((Handle) prRecHdl);
  131.             myParamPtr->SetLongGlobal((StringPtr) "\pmyPrintRecord", nil);
  132.             PrClose();
  133.             return;
  134.         }
  135.  
  136.         myParamPtr->SetLongGlobal((StringPtr) "\pmyPrinterPort", (long) myPrPort);
  137.  
  138. //  Set globals for the first and last page from the dialog box
  139.  
  140.         myParamPtr->SetLongGlobal((StringPtr) "\pfirstPage", (long) (**prRecHdl).prJob.iFstPage);
  141.         myParamPtr->SetLongGlobal((StringPtr) "\plastPage", (long) (**prRecHdl).prJob.iLstPage);
  142.     }
  143. /*
  144.   If the first parameter is false, and the print record handle is not nil, print
  145.   the postscript text passed in the second parameter.
  146. */
  147.     else
  148.     {
  149.         PrOpenPage(myPrPort,nil);
  150.         if (theErr=PrError())
  151.         {
  152.             if (theErr==iPrAbort)
  153.                 myParamPtr->SignalReturnStatus("\pAbort");
  154.             else
  155.             {
  156.                 errString= CString((char *)"PrOpenPage failed. ") + CString((long) theErr);
  157.                 myParamPtr->SignalFatalError(errString);
  158.             }
  159.             PrCloseDoc(myPrPort);
  160.             myParamPtr->SetLongGlobal((StringPtr) "\pmyPrinterPort", (long) 0);
  161.             PrClose();
  162.             DisposHandle((Handle) prRecHdl);
  163.             myParamPtr->SetLongGlobal((StringPtr) "\pmyPrintRecord", nil);
  164.             return;
  165.         }
  166.     
  167.         DoPrintPS(myParamPtr);
  168.     
  169.         PrClosePage(myPrPort);
  170.         if (theErr=PrError())
  171.         {
  172.             if (theErr==iPrAbort)
  173.                 myParamPtr->SignalReturnStatus("\pAbort");
  174.             else
  175.             {
  176.                 errString= CString((char *)"PrClosePage failed. ") + CString((long) theErr);
  177.                 myParamPtr->SignalFatalError(errString);
  178.             }
  179.             PrCloseDoc(myPrPort);
  180.             myParamPtr->SetLongGlobal((StringPtr) "\pmyPrinterPort", (long) 0);
  181.             PrClose();
  182.             DisposHandle((Handle) prRecHdl);
  183.             myParamPtr->SetLongGlobal((StringPtr) "\pmyPrintRecord", nil);
  184.             return;
  185.         }
  186.     }
  187.  
  188.     myParamPtr->SignalReturnStatus("\pOK");
  189.     return;
  190. }
  191.  
  192. void DoPrintPS(TXCMDBlock *myParamPtr)
  193. {
  194.  
  195.     const short    PostScriptBegin=190;
  196.     const short    PostScriptEnd=191;
  197.     const short    PostScriptHandle=192;
  198.     const short    PostScriptFile=193;
  199.     const short    TextIsPostScript=194;
  200.  
  201.     Rect textFrame;
  202.     Handle textHandle=myParamPtr->RecoverHandleArg(1);
  203.     register Ptr textPtr = *textHandle;
  204.     short textLen = (short) strlen(textPtr);
  205.     
  206.     
  207. /*
  208.   translate returns into spaces, zero term into return
  209. */
  210.  
  211.     for (short charNum=1;charNum<=textLen;charNum++)
  212.     {
  213.         if (*textPtr== (char)0x0D)
  214.             *textPtr=(char)' ';
  215.         textPtr++;
  216.     }
  217.     *textPtr= (char) 0x0D;
  218.     textLen+=1;
  219.  
  220. /*
  221.   Convert the PostScript text to a Pict and print. Set the font to Helvetica so
  222.   the driver does not get the idea that it must create a bitmap of a screen font.
  223. */
  224.     
  225.     SetRect(&textFrame,-8192,-8192,8192,8192);
  226.     TextFont(helvetica);
  227.     ClipRect(&textFrame);
  228.     MoveTo(20,20);
  229.     DrawString((const Str255) "\p "); // get around driver optimization
  230.     PicComment(PostScriptBegin,0,nil);
  231.     
  232.     PicComment(TextIsPostScript,0,nil);
  233.     DrawString((const Str255) "\pinitgraphics"); // so we have the default coordinate system
  234.     DrawString((const Str255) "\pjeffDict begin"); // use our private dict
  235.     PicComment(PostScriptHandle,textLen,textHandle);
  236.     DrawString((const Str255) "\pend"); // close our dict
  237.     PicComment(PostScriptEnd,0,nil);
  238.     HUnlock(textHandle);
  239.  
  240.     return;
  241.     
  242. }    
  243.